package com.itheima.d5_Recusion.beer;

/**
 * 喝酒2元1拼；4个盖子换一瓶，2个空瓶换一瓶，请问10元能喝多少瓶，剩余多少空瓶和盖子
 * 能买就买，不能买就换，不能换结束
 * // 关键点在于把瓶子和盖子当成钱 总和起来再买
 剩余的瓶子和盖子
 %4 ; %2
 */
public class Test {
    private static int totalBottle = 0 ;
    private static int restCoverNumber = 0;
    private static int restBottleNumber = 0;
    public static void main(String[] args) {
        //1. 直接买
        buy(10);
        System.out.println("总瓶子数:"+totalBottle);
        System.out.println("剩下瓶子数:"+restBottleNumber);
        System.out.println("剩下瓶盖数:"+restCoverNumber);
    }
    public static void buy(int money){
        int buyNumber = money / 2;
        totalBottle += buyNumber;
        int coverNumber = restCoverNumber+buyNumber ; //盖子数(上一次留下的)
        int bottleNumber = restBottleNumber+buyNumber ; // 瓶子数目
        // 把盖子+瓶子换成钱
        int allExchangeMoney = 0;
        if (coverNumber >= 4) {
            int coverMoney = (coverNumber / 4) * 2;
            allExchangeMoney += coverMoney;
        }
        restCoverNumber = coverNumber % 4;
        if (bottleNumber >=2){
            int bottleMoney = (bottleNumber / 2) * 2;
            allExchangeMoney +=bottleMoney;
        }
        restBottleNumber = bottleNumber % 2;
        if(allExchangeMoney >= 2){
            buy(allExchangeMoney);
        }
    }

}
